#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>

using namespace std;

int main()
{
	int n = 0;
	string str1, str2, tmp;
	cin >> n >> str1 >> str2;
	int prev1 = -1, prev2 = -1, ret = 0x3f3f3f3f;
	for (int i = 0; i < n; i++)
	{
		cin >> tmp;

		if (tmp == str1)
		{
			if (prev2 != -1)
			{
				ret = min(ret, i - prev2);
			}
			prev1 = i;
		}

		else if (tmp == str2)
		{
			if (prev1 != -1)
			{
				ret = min(ret, i - prev1);
			}
			prev2 = i;
		}
	}
	if (ret == 0x3f3f3f3f)
	{
		cout << -1 << endl;
	}
	else
	{
		cout << ret << endl;
	}

	return 0;
}